/**
 * 激活博客页面
 * @author 李华顺 <huacnlee@gmail.com>
 */
var SignupActive = {
	/**
	 * 本类的设置
	 */
	settings : {
		key : '',
		postUrl : '/',
		signinUrl : "/signin"
	},

	init: function(settings){
		$.extend(SignupActive.settings,settings);
		settings = SignupActive.settings;

		var panelSignupActive = $("#signup-active");
		/* 让文本框改动的时候，去掉错误提示的样式 */
		$("input,textarea",panelSignupActive).change(function(){
			$(this).removeClass("error");
			SignupActive.showMessage();
		});
		/* 提交按钮绑定事件 */
		$("button.submit",panelSignupActive).click(function(){
			return SignupActive.post();
		});
	},

	/**
	 * Ajax提交注冊信息
	 */
	post : function(){
		var state = new Common.ExecuteState();

		state  = SignupActive.checkForm();

		if(! state.success){
			if (state.owner) {
				state.owner.focus();
				state.owner.addClass("error");
			}
			SignupActive.showMessage(state);
			return false;
		}

		var password = $("#password");
		var passwordConfirm = $("#password-confirm");
		var blogTitle = $("#blog-title");
		var blogSlug = $("#blog-slug");


		$.ajax({
			url : SignupActive.settings.postUrl,
			type: Common.Settings.AJAX_TYPE,
			dataType: "json",
			data: "key="+ SignupActive.settings.key +"&password=" + password.val() + "&title=" + encodeURI(blogTitle.val()) + "&slug=" + blogSlug.val(),
			success: function(state){
				if(! state.success){
					SignupActive.showMessage(state);
				}
				else{
					alert("您的博客已经开通！")
					document.location.href = SignupActive.settings.signinUrl;
				}
			}
		});

		return false;

	},

	/**
	 * 验证表单是否输入正确
	 */
	checkForm : function(){
		var state = new Common.ExecuteState();
		var password = $("#password");
		var passwordConfirm = $("#password-confirm");
		var blogTitle = $("#blog-title");
		var blogSlug = $("#blog-slug");

		if(password.val().length < 6 || password.val().length > 20){
			state.message = "密码长度不符合要求，请输入6-20位。";
			state.success = false;
			state.owner = password;
			state.mark = "password";
			return state;
		}

		if(password.val() != passwordConfirm.val()){
			state.success = false;
			state.message = "两次输入的密码不一至，请检查后再提交。";
			state.owner = passwordConfirm;
			state.mark = "passwordconfirm";
			return state;
		}

		if(blogTitle.val() == ''){
			state.message = "博客名称还未输入。"
			state.success = false;
			state.owner = blogTitle;
			state.mark = "blogtitle";
			return state;
		}

		if(blogSlug.val().length < 4 || blogSlug.val().length > 100){
			state.message = "自定义域名长度要求在 4-100之间 (字母，数字，下划线与中横线)。"
			state.success = false;
			state.owner = blogSlug;
			state.mark = "blogslug";
			return state;
		}

		return state;
	},

	/**
	 * 显示消息提示
	 * @param {Object} state ExecuteState 对象
	 */
	showMessage : function(state){

		var messageBox = $("#messageBox");
		if(state && state != undefined){

			messageBox.fadeOut();
			messageBox.text(state.message);
			if (state.success) {
				messageBox.css("color","green");
			}
			else {
				messageBox.css("color","red");
			}

			messageBox.fadeIn();

		}
		else{
			messageBox.text("");
		}

	},
}
